using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Widgetsphere.Generator.Common.Util;
using Widgetsphere.Generator.Common.GeneratorFramework;

namespace Widgetsphere.Generator.Common.Forms
{
  public partial class NewModelForm : Form
  {
    public NewModelForm()
    {
      InitializeComponent();

      lblHeader.Text = "Choose model type";
      cmdOK.Enabled = false;

		AddinAppData instance =  AddinAppData.Instance;
		System.Type[] typeList = ReflectionHelper.GetCreatableObjectImplementsInterface(typeof(IGenerator), instance.ExtensionDirectory);
      cboType.Items.Add("(None)");
      cboType.SelectedIndexChanged += new EventHandler(cboType_SelectedIndexChanged);
      cboType.SelectedIndex = 0;
      foreach (System.Type type in typeList)
      {
        GeneratorAttribute att = (GeneratorAttribute)ReflectionHelper.GetSingleAttribute(typeof(GeneratorAttribute), type);
        cboType.Items.Add(att);
      }
      
    }

    private IGenerator _generatorProject = null;

    public IGenerator GeneratorProject
    {
      get { return _generatorProject; }
    }

    private void cboType_SelectedIndexChanged(object sender, System.EventArgs e)
    {
      pnlChild.Controls.Clear();
      if (cboType.SelectedIndex > 0)
      {
        GeneratorAttribute att = (GeneratorAttribute)cboType.SelectedItem;
        NewModelBaseControl ctrl = (NewModelBaseControl)ReflectionHelper.CreateInstance(att.NewModelControlType);
        ctrl.Dock = DockStyle.Fill;
        ctrl.OKEnabled += new BooleanDelegate(ctrl_OKEnabled);
        pnlChild.Controls.Add(ctrl);
      }
      else
        cmdOK.Enabled = false;
    }

    private void ctrl_OKEnabled(object sender, Widgetsphere.Generator.Common.EventArgs.BooleanEventArgs e)
    {
      cmdOK.Enabled = e.Value;
    }

    #region Button Handlers

    private void cmdOK_Click(object sender, System.EventArgs e)
    {
      if (pnlChild.Controls.Count == 0)
        return;

      NewModelBaseControl ctrl = (NewModelBaseControl)pnlChild.Controls[0];
      _generatorProject = ctrl.GeneratorProject;

      this.DialogResult = DialogResult.OK;
      this.Close();
    }

    private void cmdCancel_Click(object sender, System.EventArgs e)
    {
      this.DialogResult = DialogResult.Cancel;
      this.Close();
    }

    #endregion

  }
}
